package com.br.gvttv.adapter;

import java.util.List;

import android.content.Context;
import android.util.FloatMath;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.br.gvttv.R;
import com.br.gvttv.util.SharedPreferencesUtils;
import com.br.negocio.cargadados.CargaDadosCanal;
import com.br.negocio.entidades.CanalTv;
import com.br.negocio.entidades.CategoriaEnum;

public class CategoriaCanaisExpandableListAdapter extends BaseExpandableListAdapter {

	private LayoutInflater mInflater;
	private List<CategoriaEnum> categoriaEnumList;
	private CargaDadosCanal cargaDadosCanal;

	public CategoriaCanaisExpandableListAdapter(Context context) {
		this.mInflater = LayoutInflater.from(context);
		this.cargaDadosCanal = new CargaDadosCanal(SharedPreferencesUtils.getConfiguracaoPacoteCanalAtivos(context));
		this.categoriaEnumList = this.cargaDadosCanal.getCategoriasAtivasEnum();
	}

	@Override
	public int getGroupCount() {
		return categoriaEnumList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public CategoriaEnum getGroup(int groupPosition) {
		return categoriaEnumList.get(groupPosition);
	}

	@Override
	public CanalTv getChild(int groupPosition, int childPosition) {
		return cargaDadosCanal.getCanaisTV(categoriaEnumList.get(groupPosition)).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		CategoriaEnum categoriaEnum = getGroup(groupPosition);
		int quantidadesCanais = cargaDadosCanal.getCanaisTV(categoriaEnum).size();

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.categoria_canais_layout, null);
		}

		TextView categoriaCanalTextView = (TextView) convertView.findViewById(R.id.categoriaCanalTextView);
		categoriaCanalTextView.setText(categoriaEnum.toString());
		categoriaCanalTextView.append(" (" + quantidadesCanais + ")");

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.lista_canais_layout, null);
		}

		((GridView) convertView.findViewById(R.id.canaisTvGridView))
				.setAdapter(new GridCanaisBaseAdapter(mInflater.getContext(),
						cargaDadosCanal.getCanaisTV(getGroup(groupPosition))));

		configurarTamanhoGridView(convertView, parent);

		return convertView;
	}

	private void configurarTamanhoGridView(View convertView, ViewGroup parent) {
		int spacingDp = 10;
		int colWidthDp = 90;
		int rowHeightDp = 100;

		float density = mInflater.getContext().getResources().getDisplayMetrics().density;

		float COL_WIDTH = density * colWidthDp;
		float ROW_HEIGHT = density * rowHeightDp;
		float SPACING = density * spacingDp;

		int colCount = (int) FloatMath.floor((parent.getWidth() - (2 * SPACING)) / (COL_WIDTH + SPACING));
		int rowCount = (int) Math.ceil((((GridView) convertView.findViewById(R.id.canaisTvGridView)).getCount() + 0d) / colCount);

		int GRID_HEIGHT = Math.round(rowCount * (ROW_HEIGHT + SPACING));

		convertView.findViewById(R.id.canaisTvGridView).getLayoutParams().height = GRID_HEIGHT;
	}
}
